home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5840 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.8 KB  |  98 lines

  1. Newsgroups: comp.lang.c
  2. Path: tank.news.pipex.net!pipex!warwick!bsmail!talisker!nathan
  3. From: nathan@pact.srf.ac.uk (Nathan Sidwell)
  4. Subject: Re: How do I round and truncate floats to integers?
  5. Message-ID: <Dn4oM5.1JK@uns.bris.ac.uk>
  6. Sender: usenet@uns.bris.ac.uk (Usenet news owner)
  7. Nntp-Posting-Host: talisker.pact.srf.ac.uk
  8. Organization: Inmos
  9. X-Newsreader: TIN [version 1.2 PL2]
  10. References: <4g009b$c2n@news.tuwien.ac.at> <harmon.824447677@pegasus.montclair.edu>
  11. Date: Wed, 21 Feb 1996 13:39:41 GMT
  12.  
  13. Derek Harmon (harmon@pegasus.montclair.edu) wrote:
  14. :    It is important to note that a C typecast only tells the compiler to treat
  15. : some form of data as another form of data.  No change is made to the data.
  16. This is completely untrue. Have you actually read any C book about
  17. type casting?
  18.  
  19. Casting converts a value from one type to another. No guarantee is made
  20. about the bit pattern (or even the number of bits) remaining the same, or
  21. not remaining the same.
  22.  
  23. Now it just so happens that when the result of casting a signed int to an
  24. unsigned int is not implementation defined (ie the value to
  25. be converted is representable in both types), there is no change
  26. to the bit pattern, for a common representation of binary integers (twos
  27. complement). Many compilers extend the conversion in an implementation
  28. defined manner, so that there is never a change in the bit pattern.
  29. This may be where your confusion is coming from.
  30.  
  31. The same is true of casting pointers. casting a pointer to some type to
  32. be a pointer to some other type changes the value of the pointer. Now
  33. it just so happens that for many systems, this change in value does not
  34. cause a change in the bit pattern -- but that's entierly implementation
  35. defined.
  36.  
  37. : The binary representation of a double floating point value is very different
  38. : from that of an integer, and just telling the compiler to treat it as an int
  39. : will not solve your problem
  40.  
  41. Casting a float to an int nearly always causes a change in the bit pattern.
  42. There are possibly three cases where the cast does not cause a change
  43. for systems when the float is in 32bit IEEE and the int is 32bit twos
  44. complement. These are the value (positive) zero, some positive number
  45. and some negative number.
  46. [Which particular numbers is left as an excercise to the reader.]
  47.  
  48. :  
  49. : unsigned int real2int( double real )
  50. : {
  51. :    unsigned int whole = 0;
  52. :    unsigned int pow = 10000;        
  53. :  
  54. :    if (real > 65535.0) {
  55. Where did the value 65535.0 come from? Its incorrect for 16 bit ints
  56. (the value 32768.0 and test >= should be used). Its incorrect fo
  57. 16 bit unsigned ints (65536.0 and test >= should be used).
  58. :       fputs(stderr, "real value too large in real2int().\n");
  59. :       exit( 1 );
  60. :    }
  61. :    real = floor(real);
  62. :    while ( pow > 0 ) 
  63. :       if (real > (pow * 1.0)) {
  64. Why pow * 1.0? why not (double)pow? (actually why cast at all, the compiler
  65. will promote pow to type double anyway)
  66.  
  67. :          real -= (pow * 1.0);
  68. ditto.
  69. :          whole += pow;
  70. :       } else pow /= 10;
  71. :  
  72. :    return( whole );
  73. : }
  74.  
  75. this whole routine seems to be a very inefficient way of saying
  76.  
  77. if(real >= (double)INT_MAX + 1) {
  78.        fputs(stderr, "real value too large in real2int().\n");
  79.        exit( 1 );
  80. } else {
  81.   return floor(real);
  82. }
  83.  
  84. and a very efficient way of showing that you do not understand
  85. implicit promotion or type casting.
  86.  
  87. :    This little routine is more or less what you're looking for.  It makes
  88. : several assumptions in that real must be positive, less than what a 16-bit
  89. : unsigned int can hold (the extension to a long is straightforward), but you
  90.   ^^^^^^^^ but you're returning an int.
  91.  
  92. nathan
  93. --
  94. Nathan Sidwell                         Holder of the Xmris home page
  95. Chameleon Architecture Group at SGS-Thomson, formerly Inmos
  96. http://www.pact.srf.ac.uk/~nathan/                  Tel 0117 9707182
  97. nathan@inmos.co.uk or nathan@bristol.st.com or nathan@pact.srf.ac.uk
  98.